package com.gxa.admin.exception;


import com.gxa.o2o.core.dto.ResultDTO;
import com.gxa.o2o.core.utils.Response;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@ControllerAdvice
@Slf4j
public class MyExceptionHandler {
    /**
     * 日志记录器
     */
    //private Logger logger = LoggerFactory.getLogger(com.gxa.admin.exception.MyExceptionHandler.class);

    /**
     * 捕获系统异常
     * @param e
     * @return
     */
    @ExceptionHandler(SystemException.class)
    @ResponseBody
    public ResultDTO systemExceptionHandler(SystemException e){
        // 记录日志
        log.error(e.getMsg());
        return Response.error(e.getCode(), e.getMsg());
    }

    /**
     * 捕获系统异常-非ajax请求
     * @param e
     * @return
     */
    @ExceptionHandler(NoAjaxException.class)
    public ModelAndView noAjaxExceptionHandler(NoAjaxException e){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("error");
        // 记录日志
        log.error(e.getMsg());
        return modelAndView;
    }


    /**
     * 捕获所有异常
     * @param e
     * @return
     */
    //@ExceptionHandler(Exception.class)
    public ResultDTO exceptionHandler(Exception e){
        // 记录日志
        log.error(e.getMessage());
        return Response.error(1001, "未知异常！");
    }


}
